import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/manager/xindong_chat_singleton.dart';
import 'package:xindong/models/chat_message/xindong_custom_message.dart';
import 'package:xindong/routes/conversation/view/xindong_liaotian_message_score_container.dart';
import 'package:xindong/utils/xindong_conversion_util.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

class XinDongLiaoTianMessageText extends StatelessWidget {
  final V2TimMessage message;
  final double maxWidth;

  const XinDongLiaoTianMessageText({Key? key, required this.message, required this.maxWidth}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final isFromSelf = message.isSelf == true;
    final backgroundColor = isFromSelf ? Colors.grey.shade300 : Colors.white;

    var text = "";
    if (message.elemType == MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) {
      XinDongCustomMessage? model = XinDongConversionUtil.decodeJsonToMessageClass(message.customElem?.data ?? "");
      text = model?.content ?? "";
    } else {
      text = "${message.textElem?.text}";
    }

    return GetBuilder<XinDongChatSingleton>(
      builder: (controller) {
        return XinDongLiaoTianMessageScoreContainer(
          hidden: isFromSelf,
          message: message,
          child: Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(color: backgroundColor, borderRadius: BorderRadius.circular(10)),
            constraints: BoxConstraints(minWidth: 40, maxWidth: maxWidth),
            child: Text(
              text,
              textAlign: text.trim().length < 5 ? TextAlign.center : TextAlign.left,
              softWrap: true,
              style: const TextStyle(fontSize: 16, height: 1.2, color: DZColors.titleColor),
            ),
          ),
        );
      },
    );
  }
}
